import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import csv
import parsel
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

# # 准备好参数配置  无头浏览器
# opt = Options()
# opt.add_argument("--headless")
# opt.add_argument("--disbate-gpu")

# 创建表格
def create_csv():
    f = open('./data/' + f'{query}.csv', mode='a', encoding='utf-8-sig', newline='')  # a 是追加写模式 ， a+ 是追加读写模式
    csv_writer = csv.DictWriter(f, fieldnames=['景区', '价格', '星级', '地区', '热度', '简介', '销量', '详情链接'])
    csv_writer.writeheader()  # 写入表头
    return csv_writer

# 下载操作 线程池执行
def download_data(page):

    web.get(
        f'https://piao.qunar.com/ticket/list.htm?keyword={query}&region=&from=mps_search_suggest&page={page}')
    # 获取该页面元素
    page = web.page_source
    # css选择器解析数据
    selector = parsel.Selector(page)
    # 根据标签选择内容
    # 第一次提取， 所有景区的标签内容， 返回的是一个对象 列表//*[@id="search-list"]
    # id 可以用#定位 class 可以用.定位
    div_list = selector.css('#search-list .sight_item_detail')
    for di in div_list:
        place_name = di.css('.name::text').get()
        level = di.css('.level::text').get()
        area = di.css('.area a::text').get()
        te = di.css('.product_star_level em::attr(title)').get().replace('热度: ', '')
        temp = int(float(te) * 100)
        intro = di.css('.intro.color999::text').get()
        price = di.css('.sight_item_price em::text').get()
        sold_num = di.css('.sight_item_sold-num span::text').get()
        hr = di.css('.name::attr(href)').get()
        href = 'https://piao.qunar.com' + hr

        dit = {
            '景区': place_name,
            '星级': level,
            '地区': area,
            '热度': temp,
            '简介': intro,
            '价格': price,
            '销量': sold_num,
            '详情链接': href
        }
        csv_writer.writerow(dit)
        print(place_name, level, area, temp, intro, price, sold_num, href, sep=' | ')


if __name__ == '__main__':
    # 指定谷歌路径， 谷歌路径不是默认路径安装位置
    option = webdriver.ChromeOptions()
    option.binary_location = 'D:\Google\Chrome\Application\chrome.exe'
    web = webdriver.Chrome(chrome_options=option)
    # 1. 获取页面
    # option = webdriver.Cho
    # web = Chrome()   # 无头浏览器 options=opt
    web.get('https://piao.qunar.com/')
    time.sleep(2)

    query = input("请输入：")
    # 找到搜索框 获取想要的景点
    web.find_element(By.XPATH, value='//*[@id="searchValue"]').send_keys(f"{query}", Keys.ENTER)
    time.sleep(2)

    # 创建表
    csv_writer = create_csv()

    # 调整到新窗口
    web.switch_to.window(web.window_handles[-1])
    time.sleep(1)

    # 创建线程池
    with ThreadPoolExecutor(50) as t:
        for page in range(1, 12):
            print(f'=====================正在爬取第{page}页===============================')
            time.sleep(1.5)
            t.submit(download_data, page)